﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using MedicWeb.MedicService;

namespace MedicWeb.forms.Editors {
    public partial class InstitutionTreeEdit : SessionPage {
        protected void Page_Load(object sender, EventArgs e) {

            var mainService = new MainService();
            Form.Action = ResolveUrl("~/forms/Editors/InstitutionTreeEdit.aspx");
            string id = Request.QueryString["id"];
            if (id != null) {
                Form.Action += "?id=" + id;
                if (!IsPostBack && id != "0") {
                    Institution institution = mainService.LoadInstitution(GetSession(), Convert.ToInt32(id));
                    hfInstitutions.Value = "" + institution.Id;
                    hfLocalities.Value = "" + institution.Locality.Id;
                    hfRegions.Value = "" + institution.Locality.Region.Id;
                }
            }


            string reg = hfRegions.Value;
            string loc = hfLocalities.Value;
            string inst = hfInstitutions.Value;

            Region[] regionList = mainService.GetRegionList(GetSession(), false);
            Locality[] localityList = mainService.GetLocalityList(GetSession(), false);
            Institution[] institutionList = mainService.GetInstitutionList(GetSession(), false);

            foreach (var region in regionList) {
                var row = new TableRow();
                var cellEdit = new TableCell();
                var cellTitle = new TableCell();
                cellEdit.Controls.Add(new HyperLink() {
                    CssClass = "edit modal",
                    NavigateUrl = ResolveUrl("~/forms/Editors/Directory/Simple/SimpleEdit.aspx?directory=Region&id=" + region.Id)
                });
                cellEdit.Width = 24;
                cellTitle.Text = region.Title;
                cellTitle.CssClass = "item";

                row.ID = "" + region.Id;
                row.Cells.Add(cellEdit);
                row.Cells.Add(cellTitle);
                listRegion.Rows.Add(row);
            }
            foreach (var locality in localityList) {
                var row = new TableRow();
                var cellEdit = new TableCell();
                var cellTitle = new TableCell();
                cellEdit.Controls.Add(new HyperLink() {
                    CssClass = "edit modal",
                    NavigateUrl = ResolveUrl("~/forms/Editors/Directory/LocalityEdit.aspx?simple=true&id=" + locality.Id)
                });
                cellEdit.Width = 24;
                cellTitle.Text = locality.Title;
                cellTitle.CssClass = "item";

                row.ID = "" + locality.Id;
                row.Attributes["region"] = "" + (locality.Region != null ? locality.Region.Id : 0);
                row.Cells.Add(cellEdit);
                row.Cells.Add(cellTitle);
                listLocality.Rows.Add(row);
            }
            foreach (var institution in institutionList) {
                var row = new TableRow();
                var cellEdit = new TableCell();
                var cellTitle = new TableCell();
                cellEdit.Controls.Add(new HyperLink() {
                    CssClass = "edit modal",
                    NavigateUrl = ResolveUrl("~/forms/Editors/Directory/InstitutionEdit.aspx?simple=true&id="+institution.Id)
                });
                cellEdit.Width = 24;
                cellTitle.Text = institution.Title;
                cellTitle.CssClass = "item";

                row.ID = "" + institution.Id;
                row.Attributes["locality"] = "" + (institution.Locality != null ? institution.Locality.Id : 0);
                row.Cells.Add(cellEdit);
                row.Cells.Add(cellTitle);
                listInstitution.Rows.Add(row);
            }
        }

    }
}